﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using OpenSoftStore.BusinessLogic.Bill;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.Client.UIL.ManagePrinter;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.ManageBilling
{
    public partial class BillPay : UserControl
    {
        public T_Bill Tbill;
        public BillLogic LgcBill;
        public T_Member Tmember;
        public MemberLogic LgcMember;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public BillPay()
        {
            InitializeComponent();
            Tbill = new T_Bill();
            LgcBill = new BillLogic();
            Tmember=new T_Member();
            LgcMember=new MemberLogic();

            var moenyLogic = new MoneyLogic();
            var moenyList = moenyLogic.SqlWhere<T_Account>(" 1=1").ToList();
            var list = moenyList.Where(m => m.Name != "现金").ToList();
            cmbBankAccountId.DataSource = list;
            cmbBankAccountId.DisplayMember = "Name";
            cmbBankAccountId.ValueMember = "Id";
            nudCashPay.Focus();
        }

        private void BillPay_Load(object sender, EventArgs e)
        {
           
        }

        private void LblModuleTitle_Click(object sender, EventArgs e)
        {

        }

        public void GetBill(string id)
        {
            var data = LgcBill.SqlWhere<T_Bill>(" Id='" + id + "'").ToList();
            if (data.Any()) Tbill = data[0];

            txtBillNo.Text = Tbill.BillNo;
            txtNeedPay.Text = Tbill.NeedPay.ToString();
            nudCashPay.Select();
            nudCashPay.Focus();
            if (Tbill.PayType == "次卡消费")
            {
                lblPayType.Text = Tbill.PayType;
                nudCashPay.Enabled = false;
                nudBankPay.Enabled = false;
                nudFavorMoney.Enabled = false;
                nudValuePay.Enabled = false;
                txtOwedMoney.Text = "0";
                txtPass.ReadOnly = false;
                txtPass.Focus();
                txtPass.Select();
                return;
            }

            nudCashPay.Value = Tbill.NeedPay;
            nudMark.Value = Tbill.MarkPay;
            nudBankPay.Value = Tbill.BankPay;
            nudFavorMoney.Value = Tbill.FavorMoney;
            txtOwedMoney.Text = "0";

            Tmember = LgcBill.SqlWhere<T_Member>(" Id='" + Tbill.MemberId + "'").FirstOrDefault();
            if (Tmember == null) return;
            lblHaveValue.Text = Tmember.HaveValue + "元";
            if (Tmember.Name=="临时客户" && SPAUserConfig.GetValue("TempMemberPass") == "是")
            {
                txtPass.ReadOnly = false;
                txtPass.Focus();
            }
            if (Tmember.HaveValue >= Tbill.NeedPay && Tmember.HaveValue > 0)
            {
                nudValuePay.Value = Tbill.NeedPay;
                nudCashPay.Value = 0;
                txtPass.ReadOnly = false;
                txtPass.Focus();
            }
            else if (Tmember.HaveValue < Tbill.NeedPay && Tmember.HaveValue > 0)
            {
                nudValuePay.Value = Tmember.HaveValue;
                nudCashPay.Value = Tbill.NeedPay - Tmember.HaveValue;
                txtPass.ReadOnly = false;
                txtPass.Focus();
            }

            //dtpPayTime.Text = Tbill.PayTime;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            txtBillNo.Text = Tbill.BillNo;
            txtNeedPay.Text = Tbill.NeedPay.ToString();
            Tbill.CashPay=nudCashPay.Value;
            Tbill.MarkPay = nudMark.Value;
            Tbill.BankPay = nudBankPay.Value;
            Tbill.ValuePay = nudValuePay.Value;
            Tbill.FavorMoney = nudFavorMoney.Value;
            Tbill.OwedMoney = Convert.ToInt32(txtOwedMoney.Text);
            Tbill.BankAccountId = cmbBankAccountId.SelectedValue.ToString();
            Tmember.Id = Tbill.MemberId;
            Tmember.Pass = txtPass.Text;

            if (txtPass.ReadOnly == false && !LgcMember.ExitMemberByPass(Tmember))
            {
                FormSysMessage.ShowSuccessMsg("充值卡密码错误！");
                txtPass.Focus();
                txtPass.Text = "";
                return;
            }

            if (Tbill.OwedMoney < 0)
            {
                FormSysMessage.ShowSuccessMsg("欠款金额不能为负数！");
                return;
            }

            int row = LgcBill.Pay(Tbill,SPAUserConfig.GetValue("ValueRecordSubtractPrice"));
            string message = "";
            message = row == 0 ? "保存失败" : "保存成功";
            if (row == 0)
            {
                FormSysMessage.ShowSuccessMsg(message);
                return;
            }
            //var print=new PrintWindow();
            BillPrintWindow.Instance.SetReport(Tbill.Id);

            //print.SetReport("OpenSoftStore.Client.UIL.ManagePrinter.BillPrint.rdlc", "DataSet1", data);
            BillPrintWindow.Instance.ShowDialog();
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.OK;
            findForm.Close();
        }



        public void SetTxtOwedMoney()
        {
            Tbill.CashPay = nudCashPay.Value;
            Tbill.MarkPay = nudMark.Value;
            Tbill.BankPay = nudBankPay.Value;
            Tbill.FavorMoney = nudFavorMoney.Value;
            Tbill.ValuePay = nudValuePay.Value;
            if (Tbill.RealPay == Tbill.NeedPay) return;
            Tbill.OwedMoney = Tbill.NeedPay
                              - Tbill.CashPay
                              - Tbill.MarkPay
                              - Tbill.BankPay
                              - Tbill.ValuePay
                              - Tbill.FavorMoney;
            txtOwedMoney.Text = Convert.ToInt32(Tbill.OwedMoney).ToString();
        }


        private void nudCashPay_Leave(object sender, EventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudMark_Load(object sender, EventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudBankPay_Leave(object sender, EventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudFavorMoney_Leave(object sender, EventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudCashPay_KeyUp(object sender, KeyEventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudMark_KeyUp(object sender, KeyEventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudMark_Leave(object sender, EventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudBankPay_KeyUp(object sender, KeyEventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudFavorMoney_KeyUp(object sender, KeyEventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void nudValuePay_KeyUp(object sender, KeyEventArgs e)
        {
            txtPass.ReadOnly = nudValuePay.Value <= 0;
            SetTxtOwedMoney();
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {

        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }
    }
}
